package com.app;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

public class View {
	private JFrame jFrame = null;
	private JLabel title = null;
	private JLabel input_lbl = null;
	private JLabel output_lbl = null;
	private JTextField input_txt = null;
	private JTextField output_txt = null;
	private JButton input_btn = null;
	private JButton output_btn = null;
	private JButton draw = null;
	private ButtonGroup group = null;
	private JRadioButton radio1 = null;
	private JRadioButton radio2 = null;
	private JLabel code_lbl = null;

	public View() throws IOException {
		jFrame = new JFrame("Java项目注释提取");

		ImageIcon icon=new ImageIcon(getClass().getClassLoader().getResource("img/img.png"));
		
		jFrame.setIconImage(icon.getImage());
		
		jFrame.setLayout(new FlowLayout());

		jFrame.setSize(350, 280);
		

		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		jFrame.setLocationRelativeTo(null);
		
		jFrame.setResizable(false);

		title = new JLabel("Java项目注释提取");
		title.setFont(new Font("宋体", 0, 30));

		input_lbl = new JLabel("选择项目src的路径：");
		input_txt = new JTextField(20);
		input_txt.setEditable(false);
		input_btn = new JButton("选择");

		input_btn();

		output_lbl = new JLabel("选择注释的保存路径：");
		output_txt = new JTextField(20);
		output_txt.setEditable(false);
		output_btn = new JButton("选择");

		output_btn();

		group = new ButtonGroup();

		radio1 = new JRadioButton("GBK", true);

		radio2 = new JRadioButton("UTF-8");

		code_lbl = new JLabel("选择编码：");

		draw = new JButton("确认提取");
		draw.setBounds(0, 0, 100, 20);

		draw();

		jFrame.add(title);

		jFrame.add(input_lbl);
		jFrame.add(input_txt);
		jFrame.add(input_btn);

		jFrame.add(output_lbl);
		jFrame.add(output_txt);
		jFrame.add(output_btn);

		jFrame.add(code_lbl);
		group.add(radio1);
		group.add(radio2);

		jFrame.add(radio1);
		jFrame.add(radio2);

		jFrame.add(draw);

		jFrame.setVisible(true);
	}

	/**
	 * 确认提取
	 */
	public void draw() {
		draw.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String inputStr = input_txt.getText();
				String outputStr = output_txt.getText();
				String strCode="";
				if(radio1.isSelected()){
					strCode=radio1.getText();
				}
				else if(radio2.isSelected()){
					strCode=radio2.getText();
				}
				if (!inputStr.equals("") && !outputStr.equals("")) {



					File file = new File(outputStr);
					FileWriter fw = null;
					// 文件不存在创建
					if (!file.exists()) {
						try {
							file.createNewFile();

						} catch (IOException es) {
							es.printStackTrace();
						}

					}

					// 字符输出流
					try {
						fw = new FileWriter(file);
						Service.find(inputStr, fw, strCode);
					} catch (IOException es) {
						es.printStackTrace();
					} finally {
						try {
							fw.close();
						} catch (IOException es) {
							es.printStackTrace();
						}
					}

					JOptionPane.showMessageDialog(null, "提取成功");
				} else {
					JOptionPane.showMessageDialog(null, "没有选择目录或者文件");
				}
			}
		});
	}

	/**
	 * 注释输出选择按钮
	 */
	public void output_btn() {
		output_btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				FileNameExtensionFilter filter = new FileNameExtensionFilter("txt & Text", "txt");
				chooser.setFileFilter(filter);
				chooser.setDialogTitle("选择保存注释的路径");
				chooser.setDialogType(JFileChooser.SAVE_DIALOG);
				int returnVal = chooser.showSaveDialog(jFrame);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					output_txt.setText(chooser.getSelectedFile().getPath());
				}

			}
		});
	}

	/**
	 * 选择src目录
	 */
	public void input_btn() {
		input_btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setDialogTitle("选择src的目录");
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = chooser.showOpenDialog(jFrame);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					input_txt.setText(chooser.getSelectedFile().getPath());
				}

			}
		});
	}

	

}
